﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO.WaiGaoQiao;
using System.Data;

namespace Model.WaiGaoQiao
{
    public class WorkSupplierInfo
    {
        /// <summary>
        /// 主键，供应商ID
        /// </summary>
        public int ID
        {
            get;
            set;
        }
        /// <summary>
        /// 供应商编码
        /// </summary>
        public string Supplier_NO
        {
            get;
            set;
        }
        /// <summary>
        /// 供应商名称
        /// </summary>
        public string Supplier_Name
        {
            get;
            set;
        }
        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact_Person
        {
            get;
            set;
        }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get;
            set;
        }
        /// <summary>
        /// 供应商地址
        /// </summary>
        public string AddRess
        {
            get;
            set;
        }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark
        {
            get;
            set;
        }
        /// <summary>
        /// 录入时间
        /// </summary>
        public DateTime RecordDateTime
        {
            get;
            set;
        }
        private DataAccessWork_Supplier dar;
        private int Suppliercount;
        public int _Suppliercount
        {
            get { return Suppliercount; }
        }
        public WorkSupplierInfo(bool isgetcount)
        {
            dar = new DataAccessWork_Supplier();
            if (isgetcount)
            {
                Suppliercount = GetSupplierCount();
            }
        }
        public WorkSupplierInfo()
        { }
        /// <summary>
        /// 获取供应商数量
        /// </summary>
        /// <returns></returns>
        public int GetSupplierCount()
        {
            return dar.GetSupplierCount();
        }

        /// <summary>
        /// 获取供应商信息（分页）
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        public IList<WorkSupplierInfo> SelSupplierInfo(int pagesize, int startindex, string strquery)
        {
            IList<WorkSupplierInfo> supplist = new List<WorkSupplierInfo>();
            WorkSupplierInfo suppinfo;
            DataSet ds = dar.SelSupplierInfo(pagesize, startindex, strquery);
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                suppinfo = new WorkSupplierInfo();

                suppinfo.ID = Convert.ToInt32(item["ID"]);
                suppinfo.Supplier_NO = item["Supplier_NO"].ToString();
                suppinfo.Supplier_Name = item["Supplier_Name"].ToString();
                suppinfo.Contact_Person = item["Contact_Person"].ToString();
                suppinfo.Phone = item["Phone"].ToString();
                suppinfo.AddRess = item["AddRess"].ToString();
                suppinfo.Remark = item["Remark"].ToString();
                suppinfo.RecordDateTime = !string.IsNullOrEmpty(item["RecordDateTime"].ToString()) ? Convert.ToDateTime(item["RecordDateTime"]) : DateTime.Now;

                supplist.Add(suppinfo);
            }
            return supplist;
        }

        /// <summary>
        /// 保存供应商(新增界面下)
        /// </summary>
        /// <param name="rolepara"></param>
        /// <returns></returns>
        public int SaveSupplier(WorkSupplierInfo supp)
        {
            object[] supppara = new object[] { supp.Supplier_NO, supp.Supplier_Name, supp.Contact_Person, supp.Phone, supp.AddRess, supp.Remark };
            return dar.SaveSupplier(supppara);
        }

        /// <summary>
        /// 保存供应商(修改界面下)
        /// </summary>
        /// <param name="rolepara"></param>
        /// <returns></returns>
        public int SaveSupplierEdit(WorkSupplierInfo supp)
        {
            object[] supppara = new object[] { supp.ID, supp.Supplier_NO, supp.Supplier_Name, supp.Contact_Person, supp.Phone, supp.AddRess, supp.Remark };
            return dar.SaveSupplierEdit(supppara);
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteSupplier(string id)
        {
            return dar.DeleteSupplier(id);
        }

        /// <summary>
        /// 根据编号获取一条供应商信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public WorkSupplierInfo GetSupplierInfoOne(string supplierId)
        {
            try
            {
                dar = new DataAccessWork_Supplier();
                DataSet ds = dar.GetSupplierInfoOne(supplierId);
                WorkSupplierInfo info = new WorkSupplierInfo();
                info.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                info.Supplier_NO = ds.Tables[0].Rows[0]["Supplier_NO"].ToString();
                info.Supplier_Name = ds.Tables[0].Rows[0]["Supplier_Name"].ToString();
                info.Contact_Person = ds.Tables[0].Rows[0]["Contact_Person"].ToString();
                info.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                info.AddRess = ds.Tables[0].Rows[0]["AddRess"].ToString();
                info.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                return info;
            }
            catch 
            {
                return new WorkSupplierInfo();
            }
        }

        /// <summary>
        /// 根据ID获取供应商的名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetSupplierName(int id)
        {
            return DataAccessSupplier.GetSupplierName(id);
        }

        public bool CheckSupplierNo(string Supplier_NO, string OldSupplier_NO, string AddEdit)
        {
            int count = dar.CheckSupplierNo(Supplier_NO, OldSupplier_NO, AddEdit);
            return count > 0 ? false : true;
        }
    }
}
